/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;

/**
 *
 * @author gravena
 */
public class DownloadProperties {
    private int nConn = 0;
    private long downloadedParts;
    //private PriorityQueue<Long> parts;
    private ArrayList<Long> parts;
    private boolean writing;
    private long amoutDownloaded;
    private long downloadRate;
    
    private long window;
    
    private HashMap<Long, Long> rates;
    
    public DownloadProperties() {
        this.rates = new HashMap<>();
    }

    public int getnConn() {
        return nConn;
    }

    public void setnConn(int nConn) {
        this.nConn = nConn;
    }

    public long getDownloadedParts() {
        return downloadedParts;
    }

    public void setDownloadedParts(long downloadedParts) {
        this.downloadedParts = downloadedParts;
    }

    public ArrayList<Long> getParts() {
        return parts;
    }

    public void setParts(ArrayList<Long> parts) {
        this.parts = parts;
    }

    public boolean isWriting() {
        return writing;
    }

    public void setWriting(boolean writing) {
        this.writing = writing;
    }

    public long getAmoutDownloaded() {
        return amoutDownloaded;
    }

    public void setAmoutDownloaded(long amoutDownloaded) {
        this.amoutDownloaded = amoutDownloaded;
    }

    public long getDownloadRate() {
        return downloadRate;
    }

    public void setDownloadRate(long downloadRate) {
        this.downloadRate = downloadRate;
    }
    
    public String humanReadableByteRate() {
        long bytes = this.downloadRate;
        
        if(bytes == 0) {
            return "0 bytes/s";
        }
        int unit = 1024;
        if (bytes < unit) return bytes + " Bytes/s";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = ("KMGTPE").charAt(exp-1)+"";
        return String.format("%.1f %sbytes/s", bytes / Math.pow(unit, exp), pre);
    }
    
    public void addRate(long index, long rate) {
        Long I = new Long(index);
        Long R = new Long(rate);
        
        this.rates.put(I, R);
    }
    
    public Long removeRate(long index) {
        return this.rates.remove(new Long(index));
    }
    
    public void updateRate(long index, long rate) {                
        Long p = removeRate(index);
        addRate(index, rate+p.longValue());        
    }
    
    public void clearRate() {
        this.rates.clear();
    }
    
    public long getRate() {
        Iterator<Long> it = this.rates.values().iterator();
        
        long soma = 0;
        while(it.hasNext()) {
            soma += it.next().longValue();
        }
        
        return soma;
    }

    public long getWindow() {
        return window;
    }

    public void setWindow(long window) {
        this.window = window;
    }
    
    
}
